package com.ysstech.reportworld.service.impl;

import com.ysstech.reportworld.entity.DataReport;
import com.ysstech.reportworld.mapper.DataReportMapper;
import com.ysstech.reportworld.service.DataReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 基础数据对应报表信息 服务实现类
 * </p>
 *
 * @author lishuangliang
 * @since 2022-12-02
 */
@Service
public class DataReportServiceImpl extends ServiceImpl<DataReportMapper, DataReport> implements DataReportService {

    @Override
    public Map<String, List<String>> getDataReportMap() throws Exception {
        // 所属报表 code和数据，对应一个list
        List<DataReport> reportList = this.list();
        if (CollectionUtils.isEmpty(reportList)) {
            return null;
        }
        Map<String, List<String>> mapDataReport = new HashMap<>();
        for (DataReport report : reportList) {
            if (null == report || StringUtils.isBlank(report.getDataId()) || StringUtils.isBlank(report.getDataCode())) {
                continue;
            }
            String key = report.getDataCode() + "\t" + report.getDataId();
            if (mapDataReport.containsKey(key)) {
                List<String> list = mapDataReport.get(key);
                list.add(report.getReportId());
                mapDataReport.put(key, list);
            } else {
                List<String> list = new ArrayList<>();
                list.add(report.getReportId());
                mapDataReport.put(key, list);
            }
        }
        return mapDataReport;
    }
}
